package com.wzh.himalaya.adapters;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.wzh.himalaya.R;
import com.ximalaya.ting.android.opensdk.model.track.Track;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * className:DetailAdapter
 * Author:WZH
 * Time:2022-03-29
 */
public class DetailListAdapter extends RecyclerView.Adapter<DetailListAdapter.InnerHolder> {

    private List<Track> mDetailData = new ArrayList<>();
    //格式化时间
    private SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    private SimpleDateFormat mDurationFormat = new SimpleDateFormat("mm:ss");
    private ItemClickListener mItemClickListener=null;


    @NonNull
    @Override
    public InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_album_detail,parent,false);

        return new InnerHolder(itemView) ;
    }

    @Override
    public void onBindViewHolder(@NonNull InnerHolder holder, @SuppressLint("RecyclerView") int position) {
        //找到控件，设置数据
        View itemView = holder.itemView;
        //顺序di
        TextView orderTv = itemView.findViewById(R.id.item_id_text);
        //标题
        TextView titleTv = itemView.findViewById(R.id.trick_title_text);
        //播放次数
        TextView platCount =itemView.findViewById(R.id.track_play_count_text);
        //时长
        TextView durationTv = itemView.findViewById(R.id.track_play_time_text);
        //更新日期
        TextView updateDateTv = itemView.findViewById(R.id.update_time_text);

        //设置数据
        Track track = mDetailData.get(position);
        orderTv.setText((position+1)+"");
        titleTv.setText(track.getTrackTitle());
        platCount.setText(track.getPlayCount()+"");

        int durationMil = track.getDuration()*1000;
        String duration = mDurationFormat.format(durationMil);

        durationTv.setText(duration+"");

        String   updateTimeText  = mSimpleDateFormat.format(track.getUpdatedAt());
        updateDateTv.setText(updateTimeText);

        //设置一个item的点击事件
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mItemClickListener != null) {
                    //参数需要有列表位置
                    mItemClickListener.onItemClick(mDetailData,position);
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return mDetailData.size();
    }

    public void setData(List<Track> tracks) {
        //清除原来的数据
        mDetailData.clear();
        //添加新的数据
        mDetailData.addAll(tracks);
        //更新UI
        notifyDataSetChanged();

    }

    public class InnerHolder extends RecyclerView.ViewHolder {
        public InnerHolder(@NonNull View itemView) {
            super(itemView);
        }
    }

    public void setItemClickListener(ItemClickListener listener) {
        this.mItemClickListener = listener;
    }


    public interface ItemClickListener{
        void onItemClick(List<Track> detailData, int position);
    }
}
